package com.tencent.albert.weixin.driver.utils;

import com.tencent.albert.weixin.driver.bean.WxRespBean;
import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * author  living.li
 * date    2015/6/26.
 */
public class EcryptionUtil {
    //AES
    public static class AES {
        /**
         * ?????
         */
        private static final String KEY_ALGORITHM = "AES";

        private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

        /**
         * ??????
         *package com.tencent.albert.weixin.driver.bean;

         import org.apache.commons.lang3.builder.ToStringBuilder;

         /**
         * Created by albertzhu on 15/2/15.
         */
        public class WxUserInfo extends WxRespBean {
            public String getOpenid() {
                return openid;
            }

            public void setOpenid(String openid) {
                this.openid = openid;
            }

            public String getNickname() {
                return nickname;
            }

            public void setNickname(String nickname) {
                this.nickname = nickname;
            }

            public String getSex() {
                return sex;
            }

            public void setSex(String sex) {
                this.sex = sex;
            }

            public String getProvince() {
                return province;
            }

            public void setProvince(String province) {
                this.province = province;
            }

            public String getCity() {
                return city;
            }

            public void setCity(String city) {
                this.city = city;
            }

            public String getCountry() {
                return country;
            }

            public void setCountry(String country) {
                this.country = country;
            }

            public String getHeadimgurl() {
                return headimgurl;
            }

            public void setHeadimgurl(String headimgurl) {
                this.headimgurl = headimgurl;
            }

            public String[] getPrivilege() {
                return privilege;
            }

            public void setPrivilege(String[] privilege) {
                this.privilege = privilege;
            }

            public String getUnionid() {
                return unionid;
            }

            public void setUnionid(String unionid) {
                this.unionid = unionid;
            }

            String openid;//用户的唯一标识
            String nickname;//用户昵称
            String sex;//用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
            String province;//用户个人资料填写的省份
            String city;//普通用户个人资料填写的城市
            String country;//国家，如中国为CN
            String headimgurl;//用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空。若用户更换头像，原有头像URL将失效。
            String[] privilege;//用户特权信息，json 数组，如微信沃卡用户为（chinaunicom）
            String unionid;//只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。详见：获取用户个人信息（UnionID机制）

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this);
            }
        }
        /*
        * @param key ?????????
         * @return ???
         */
        private static Key toKey(byte[] key) {
            //???????
            return new SecretKeySpec(key, KEY_ALGORITHM);
        }

        /**
         * ????
         *
         * @param data ??????????
         * @param key  ???
         * @return byte[]   ????????
         * @throws Exception
         */
        public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
            Key k = toKey(key);
            //?????
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            //????????????????????????
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, k, ivSpec);
            //??в???
            byte[] encode = cipher.doFinal(data);
            return encode;
        }

        public static byte[] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
            return decrypt(data, 0, key, iv);
        }

        /**
         * ????
         *
         * @param data ??????????
         * @param key  ???
         * @return byte[]   ????????
         * @throws Exception
         */
        public static byte[] decrypt(byte[] data, int offset, byte[] key, byte[] iv) throws Exception {
            if (data == null || data.length < 16) {
                throw new Exception("data illegal");
            }
            Key k = toKey(key);
            //?????
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            //????????????????????????
            cipher.init(Cipher.DECRYPT_MODE, k, ivSpec);
            //??в???
            return cipher.doFinal(data, offset, data.length - offset);
        }
    }
}
